package cn.dakaqi.services.impl;

import cn.dakaqi.component.EntityManagerUtil;
import cn.dakaqi.dao.OrganizationDao;
import cn.dakaqi.entities.Organization;
import cn.dakaqi.services.OrganizationService;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import cn.dakaqi.vo.db.OrgTestVo;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/6/14.
 */
@Component(value = "organizationService")
public class OrganizationServiceImpl implements OrganizationService
{
    @Autowired
    OrganizationDao organizationDao;

    @Resource
    private EntityManagerUtil em;

    @Override
    public List<OrgTestVo> findByContation(String code,String onePass,String phone,String orderByStr, int pageIndex,int pageSize){
        StringBuffer queryStr = new StringBuffer(
                "SELECT c.ORGID as orgId, c.ORGCODE as orgCode, c.ONEPASS as onePass, c.PHONE as phone, c.STATUS as status FROM "
                        + "U_ORGANIZATION c");

        ArrayList<String> conditionArr = new ArrayList<>();

        if (code != null && code.length() > 0) {
            conditionArr.add(" ( c.ORGCODE = '" + code + "')");
        }

        if (onePass != null && onePass.length() > 0) {
            conditionArr.add(" ( c.ONEPASS = '" + onePass + "')");
        }

        if (phone != null && phone.length() > 0) {
            conditionArr.add(" ( c.PHONE = '" + phone + "')");
        }

        if (conditionArr.size() > 0) {
            queryStr = queryStr.append(" where ")
                    .append(StringUtils.join(conditionArr.toArray(new String[conditionArr.size()]), " AND "));
        }

        if (orderByStr != null && !"".equals(orderByStr)) {
            queryStr.append(orderByStr);
        }

        if (pageIndex != 0 && pageSize != 0) {
            queryStr.append(" limit ").append(pageSize).append(" offset ").append((pageIndex - 1) * pageSize);
        }
//        OrgTestVo orgTestVo = new OrgTestVo();
//        orgTestVo.setOnePass("1");
        List<OrgTestVo> result = em.createNativeQueryList(queryStr.toString(), Transformers.aliasToBean(OrgTestVo.class));

        return result;
    }

    @Override
    public List<Organization> findByName(String name, int pageNumber) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("orgname", new SearchFilter("orgname", SearchFilter.Operator.LIKE, name));
        Specification<Organization> spec = DynamicSpecifications.bySearchFilter(filters.values(), Organization.class);
        return this.organizationDao.findAll(spec);
    }

    @Override
    public Organization findByOnePass(String onePass) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("onepass", new SearchFilter("onepass", SearchFilter.Operator.EQ, onePass));
        Specification<Organization> spec = DynamicSpecifications.bySearchFilter(filters.values(), Organization.class);
        List<Organization> list = this.organizationDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }
}
